/**
 * Title: Hyperbolic engine
 * Description: jFamilyTree Engine
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Settings
 * 
 * @author Bernard Bou
 */
public class Settings
{
	// V I E W

	/**
	 * Background color
	 */
	public Color theBackColor;

	/**
	 * Foreground color
	 */
	public Color theForeColor;

	/**
	 * Default background image
	 */
	public String theBackgroundImage;

	// fonts
	/**
	 * Font face
	 */
	public String theFontFace;

	/**
	 * Font size
	 */
	public Integer theFontSize;

	/**
	 * Font size decrement step
	 */
	public Integer theFontSizeStep;

	// T R E E

	/**
	 * Tree orientation
	 */
	public String theOrientation;

	/**
	 * Expansion
	 */
	public Double theExpansion;

	/**
	 * Sweep
	 */
	public Double theSweep;

	/**
	 * Whether orientation is preserved across transforms
	 */
	public Boolean thePreserveOrientationFlag;

	/**
	 * Whether edges are represented in a hyperbolic fashion
	 */
	public Boolean theHyperbolicEdgesFlag;

	// B E H A V I O U R

	// control and status
	/**
	 * Whether toolbar is enabled
	 */
	public Boolean theHasToolbarFlag;

	/**
	 * Whether status bar is enabled
	 */
	public Boolean theHasStatusbarFlag;

	/**
	 * Whether popup menus are enabled
	 */
	public Boolean theHasPopUpMenuFlag;

	/**
	 * Whether tooltips are enabled
	 */
	public Boolean theHasToolTipFlag;

	/**
	 * Whether tooltips display contents
	 */
	public Boolean theToolTipDisplaysContentFlag;

	// focus
	/**
	 * Whether hovering on node triggers gaining focus
	 */
	public Boolean theFocusOnHoverFlag;

	/**
	 * Focus
	 */
	public String theFocus;

	// initial move
	/**
	 * Initial move to x position (0,1)
	 */
	public Double theXMoveTo;

	/**
	 * Initial move to y position (0,1)
	 */
	public Double theYMoveTo;

	// shift
	/**
	 * Painting shift on x
	 */
	public Double theXShift;

	/**
	 * Painting shift on y
	 */
	public Double theYShift;

	// N O D E S

	// color
	/**
	 * Node default background color
	 */
	public Color theNodeBackColor;

	/**
	 * Node default foreground color
	 */
	public Color theNodeForeColor;

	// images
	/**
	 * Default node image
	 */
	public String theDefaultNodeImage;

	// T R E E . E D G E S

	/**
	 * Tree edge default color
	 */
	public Color theTreeEdgeColor;

	/**
	 * Tree edge default style
	 */
	public Integer theTreeEdgeStyle;

	/**
	 * Tree edge default image
	 */
	public String theDefaultTreeEdgeImage;

	// E D G E S

	/**
	 * Edge default color
	 */
	public Color theEdgeColor;

	/**
	 * Edge default style
	 */
	public Integer theEdgeStyle;

	/**
	 * Default edge image
	 */
	public String theDefaultEdgeImage;

	// M E N U

	/**
	 * Menu
	 */
	public List<MenuItem> theMenu;

	// C O N S T R U C T O R

	/**
	 * Constructor
	 */
	public Settings()
	{
	}

	/**
	 * Load settings from properties
	 * 
	 * @param theseProperties
	 *        properties
	 * @throws Exception
	 */
	public void load(Properties theseProperties) throws Exception
	{
		String thisParam;
		Color thisColor;
		Integer thisStyle;

		// top
		thisParam = theseProperties.getProperty("toolbar");
		if (thisParam != null)
			this.theHasToolbarFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("statusbar");
		if (thisParam != null)
			this.theHasStatusbarFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("popupmenu");
		if (thisParam != null)
			this.theHasPopUpMenuFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("tooltip");
		if (thisParam != null)
			this.theHasToolTipFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("tooltip-displays-content");
		if (thisParam != null)
			this.theToolTipDisplaysContentFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("focus");
		if (thisParam != null)
			this.theFocus = thisParam;
		thisParam = theseProperties.getProperty("focus-on-hover");
		if (thisParam != null)
			this.theFocusOnHoverFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("xmoveto");
		if (thisParam != null)
			this.theXMoveTo = Double.valueOf(thisParam).doubleValue();
		thisParam = theseProperties.getProperty("ymoveto");
		if (thisParam != null)
			this.theYMoveTo = Double.valueOf(thisParam).doubleValue();
		thisParam = theseProperties.getProperty("xshift");
		if (thisParam != null)
			this.theXShift = Double.valueOf(thisParam).doubleValue();
		thisParam = theseProperties.getProperty("yshift");
		if (thisParam != null)
			this.theYShift = Double.valueOf(thisParam).doubleValue();

		// tree
		thisParam = theseProperties.getProperty("orientation");
		if (thisParam != null)
			this.theOrientation = thisParam;
		thisParam = theseProperties.getProperty("expansion");
		if (thisParam != null)
			this.theExpansion = Double.valueOf(thisParam).doubleValue();
		thisParam = theseProperties.getProperty("sweep");
		if (thisParam != null)
			this.theSweep = Double.valueOf(thisParam).doubleValue();
		thisParam = theseProperties.getProperty("preserve-orientation");
		if (thisParam != null)
			this.thePreserveOrientationFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("fontface");
		if (thisParam != null)
			this.theFontFace = thisParam;
		thisParam = theseProperties.getProperty("fontsize");
		if (thisParam != null)
			this.theFontSize = Integer.valueOf(thisParam).intValue();
		thisParam = theseProperties.getProperty("fontsizestep");
		if (thisParam != null)
			this.theFontSizeStep = Integer.valueOf(thisParam).intValue();
		thisColor = Utils.stringToColor(theseProperties.getProperty("backcolor"));
		if (thisColor != null)
			this.theBackColor = thisColor;
		thisColor = Utils.stringToColor(theseProperties.getProperty("forecolor"));
		if (thisColor != null)
			this.theForeColor = thisColor;
		thisParam = theseProperties.getProperty("background.image");
		if (thisParam != null)
			this.theBackgroundImage = thisParam;

		// nodes
		thisColor = Utils.stringToColor(theseProperties.getProperty("node.backcolor"));
		if (thisColor != null)
			this.theNodeBackColor = thisColor;
		thisColor = Utils.stringToColor(theseProperties.getProperty("node.forecolor"));
		if (thisColor != null)
			this.theNodeForeColor = thisColor;
		thisParam = theseProperties.getProperty("node.image");
		if (thisParam != null)
			this.theDefaultNodeImage = thisParam;

		// edges
		thisParam = theseProperties.getProperty("edge.hyperbolic");
		if (thisParam != null)
			this.theHyperbolicEdgesFlag = Boolean.valueOf(thisParam);
		thisParam = theseProperties.getProperty("edge.image");
		if (thisParam != null)
			this.theDefaultEdgeImage = thisParam;
		thisParam = theseProperties.getProperty("tree.edge.image");
		if (thisParam != null)
			this.theDefaultTreeEdgeImage = thisParam;
		thisColor = Utils.stringToColor(theseProperties.getProperty("edge.color"));
		if (thisColor != null)
			this.theEdgeColor = thisColor;
		thisColor = Utils.stringToColor(theseProperties.getProperty("tree.edge.color"));
		if (thisColor != null)
			this.theTreeEdgeColor = thisColor;
		thisStyle = Utils.parseStyle(theseProperties.getProperty("edge.stroke"), theseProperties.getProperty("edge.fromterminator"), theseProperties.getProperty("edge.toterminator"), theseProperties.getProperty("edge.hidden"));
		if (thisStyle != null)
			this.theEdgeStyle = thisStyle;
		thisStyle = Utils.parseStyle(theseProperties.getProperty("tree.edge.stroke"), theseProperties.getProperty("tree.edge.fromterminator"), theseProperties.getProperty("tree.edge.toterminator"), theseProperties.getProperty("tree.edge.hidden"));
		if (thisStyle != null)
			this.theTreeEdgeStyle = thisStyle;

		// menu
		for (int i = 0; (thisParam = theseProperties.getProperty("menuitem" + i)) != null; i++)
		{
			// label;action;link;target;matchTarget;matchScope;matchMode
			String[] theseFields = thisParam.split(";");
			if (theseFields.length != 7)
				continue;

			// menu item
			MenuItem thisMenuItem = new MenuItem();
			thisMenuItem.theLabel = theseFields[0];
			thisMenuItem.theLink = theseFields[2];
			thisMenuItem.theTarget = theseFields[3];
			thisMenuItem.theMatchTarget = theseFields[4];
			Utils.parseMenuItem(thisMenuItem, theseFields[1], theseFields[5], theseFields[6]); // action,scope,mode

			// add
			if (this.theMenu == null)
				this.theMenu = new ArrayList<MenuItem>();
			this.theMenu.add(thisMenuItem);
		}
	}
}
